<template>
  <!--
    基本表单验证
      1：表单元素值的绑定
      2：el-form
           model:整个表单元素的值（对象）
           rules:整个表单的规则（对象）
           rules:{
             prop的值:[
               {required:true,message:'信息',trigger:'blur/change'},
               {min:最小长度，max:'最大长度',message:'错误信息',trigger:'blur/change'},
               {pattern:正则,message:'错误信息',trigger:'blur/change'}
             ]
           }
      3:el-form-item
           prop:它的值表单元素值的字段名
   -->
  <div>
    <el-form ref="form" label-width="80px" :model="form" :rules="rules">
      <el-form-item label="用户名" prop="username">
        <el-input v-model="form.username" />
      </el-form-item>
      <el-form-item label="验证码" prop="code">
        <el-input v-model="form.code" />
      </el-form-item>
    </el-form>
    <el-button @click="submit">提交</el-button>
  </div>
</template>
<script>
export default {
  data() {
    return {
      form: {
        username: '',
        code: ''
      },
      rules: {
        username: [
          { required: true, message: '必填', trigger: 'change' },
          { min: 2, max: 8, message: '请输入2-8位用户名', trigger: 'change' }
        ],
        code: [
          { required: true, message: '必填', trigger: 'change' },
          { pattern: /^\w{6}$/, message: '请输入6位验证码', trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    async submit() {
      // this.$refs.form.validate((result) => {
      //   console.log(result)
      // })
      // this.$refs.form.validate().then(() => {
      //   console.log('验证成功')
      // }).catch(() => {
      //   console.log('验证失败')
      // })
      await this.$refs.form.validate()
      console.log('成功')
    }
  }
}
</script>
<style>
</style>>
